<?php use_javascript('jquery-1.7.1.min.js') ?>
<?php
//busqueda de rango del objetivo
$idobjetivo = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_obs');
$q = Doctrine_Query::create()
        ->from('objetivo o')
        ->where('o.id_ob=?', $idobjetivo);
$objetivoseleccionado = $q->fetchOne();
$idproyecto = $objetivoseleccionado->getIdPro();
//busqueda de las descripciones de cada actividad de acuerdo al tipo de recurso
$idac = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_ac');
$idre = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_tr');
$idfo = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_fo');

$listadescripciones = Doctrine::getTable('descripciongeneral')->createQuery('d')
        ->where('d.id_pro = ?', $idproyecto)
        ->andWhere('d.id_tp = ?', $idre)
        ->execute();
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />        



        <script type="text/javascript">
           
            
            $(document).ready(function()
            {
                $(".edit_trdes").click(function()
                {
                    var ID=$(this).attr('id');
                    $("#first_"+ID).hide();
                    $("#last_"+ID).hide();
                    $("#first_input_"+ID).show();
                    $("#last_input_"+ID).show();
                }).change(function()
                {
                    var ID=$(this).attr('id');
                    var first=$("#first_input_"+ID).val();
                    var last=$("#last_input_"+ID).val();
                    var dataString = 'id='+ ID +'&firstname='+first+'&lastname='+last;
                    
                    

                    if(first.length && last.length>0)
                    {
                        $.ajax({
                            type: "POST",
                            url: "<?php echo url_for('valores/graba2?id=ID&firstname=first&lastname=last'); ?>",
                            data: dataString,
                            cache: false,
                            success: function(html)
                            {

                                $("#first_"+ID).html(first);
                                $("#last_"+ID).html(last);
                            }
                        });
                    }
                    else
                    {
                        alert('Enter something.');
                    }
                    $("#first_"+ID).html('<?php echo image_tag('load.gif') ?>');
                    $("#last_"+ID).html('<?php echo image_tag('load.gif') ?>');
                    $("#first_"+ID).html(first);
                    $("#last_"+ID).html(last);
                    

                });

                $(".editboxdes").mouseup(function() 
                {
                    return false
                });

                $(document).mouseup(function()
                {
                    $(".editboxdes").hide();
                    $(".textdes").show();
                });
                  
                  
                  
                  
                $(".edit_tr").click(function()
                {                   
                
                    var ID=$(this).attr('id');
                    $("#firste_"+ID).hide();                    
                    $("#firste_input_"+ID).show();                      
                    
                    
                }).change(function()
                {
                    var ID=$(this).attr('id');
                    var first=$("#firste_input_"+ID).val();                    
                    var dataString = 'id='+ ID +'&firstname='+first;                    
                    if(first.length >0)
                    {
                        if(isNaN($("#firste_input_"+ID).val())) {
                            alert("Solo se amite n&uacute;meros");			
                        }else{
                            if(($("#firste_input_"+ID).val())>=0){
                                
                            $.ajax({                         
                                url: '<?php echo url_for('valores/graba?id=ID&firstname=first'); ?>',
                                type: "POST",
                                data: dataString,
                                cache: false,                                 
                                success: function(html)
                                {
                                    $("#firste_"+ID).html(first);                                                                
                                }                            
                            });
                            
                            $("#firste_"+ID).html('<?php echo image_tag('load.gif') ?>');
                            $("#firste_"+ID).html(first);
                           }else{
                              alert("No se amite n&uacute;meros negativos"); 
                           }
                        }                                             
                    }
                    else
                    {
                        alert('Campo vacio');
                    }
                });
                $(".editbox").mouseup(function() 
                {
                    return false
                });
                $(document).mouseup(function()
                {
                    $(".editbox").hide();
                    $(".text").show();
                });
            });         
        </script>
        <style>
            body
            {
                font-family:Arial, Helvetica, sans-serif;
                font-size:14px;
            }
            .editbox
            {
                display:none
            }
            td
            {
                padding:7px;
            }
            .editbox
            {
                font-size:12px;
                width:auto;
                background-color:#ffffcc;

                border:solid 1px #000;
                padding:4px;
            }
            .edit_tr:hover
            {
                background:url('/images/edit.png') right no-repeat #80C8E5;
                cursor:pointer;
            }
            
            .edit_td:hover
            {
                background:url('/images/edit.png') right no-repeat #80C8E5;
                cursor:pointer;
            }


            .editboxdes
            {
                display:none
            }
            td
            {
                padding:7px;
            }
            .editboxdes
            {
                font-size:12px;
                width:auto;
                background-color:#ffffcc;

                border:solid 1px #000;
                padding:4px;
            }
            .edit_trdes:hover
            {
                background:url('/images/edit.png') right no-repeat #80C8E5;
                cursor:pointer;
            }


            th
            {
                font-weight:bold;
                text-align:left;
                padding:4px;
            }
            .head
            {
                background-color:#333;
                color:#FFFFFF

            }

        </style>

    </head>

    <div id="margentabla">
        <table id="contextual" border="1" style="border-color:#00000;">
            <thead>
                <?php include_partial('tdanio') ?>
            </thead>          
            <tbody>
                <?php
                if (count($listadescripciones)>0) {
                    foreach ($listadescripciones as $descr):
                        ?>
                        <tr>
                            <td id="<?php echo $descr->getIdDg() ?>"  class="edit_trdes">
                                <span id="first_<?php echo $descr->getIdDg() ?>" class="textdes"><?php echo $descr->getCodigo() ?></span>                                        
                                <textarea  rows="1" cols="25"   class="editboxdes" id="first_input_<?php echo $descr->getIdDg() ?>"><?php echo $descr->getCodigo() ?></textarea>
                            </td>                                    
                            <td id="<?php echo $descr->getIdDg() ?>"  class="edit_trdes">
                                <span id="last_<?php echo $descr->getIdDg() ?>" class="textdes"><?php echo $descr->getNombre() ?></span>                                         
                                <textarea  rows="2" cols="50"   class="editboxdes" id="last_input_<?php echo $descr->getIdDg() ?>"><?php echo $descr->getNombre() ?></textarea>
                            </td>
                            <?php
                            $listavalfond = Doctrine::getTable('valor')->createQuery('v')
                                    ->where('v.id_dg = ?', $descr->getIdDg())
                                    ->andwhere('v.id_ac = ?', $idac)
                                    ->andwhere('v.id_fo = ?', $idfo)
                                    ->execute();
                            $total = 0;
                            foreach ($listavalfond as $vals):
                                $total = $total + $vals->getVal();
                                ?>
                                <td align="center" id="<?php echo $vals->getIdVa() ?>" class="edit_tr" >
                                    <span id="firste_<?php echo $vals->getIdVa() ?>" class="text"><?php echo round($vals->getVal(),2) ?></span>
                                    <input  type="text" size="5"  value="<?php echo round($vals->getVal(),2) ?>" class="editbox" id="firste_input_<?php echo $vals->getIdVa() ?>" />
                                </td>  


                            <?php endforeach; ?>
                            <td align="center"><?php echo $total ?></td>
                        </tr>
                        <?php
                    endforeach;
                }else {
                    $idobjetivo = $this->user = sfContext::getInstance()->getUser()->getAttribute('id_obs');
                    $q = Doctrine_Query::create()
                            ->from('objetivo o')
                            ->where('o.id_ob=?', $idobjetivo);
                    $objetivoseleccionado = $q->fetchOne();
                    $fi = explode("-", $objetivoseleccionado->getPeriodos()->getFechai());
                    $ff = explode("-", $objetivoseleccionado->getPeriodos()->getFechaf());
                    $ai = $fi[0];
                    $mi = $fi[1];
                    $af = $ff[0];
                    $mf = $ff[1];
                    $ainicial = $ai;
                    $afinal = $af;
                    $minicial = $mi;
                    $mfinal = $mf;
//
                    $aux = $ainicial;
                    $imes = 0;
                    $medmes = 0;
                    $fmes = 0;
                    $tomes = 0;
                    $cmes = 0;
                    if ($ainicial == $afinal) {
                        if ($minicial == $mfinal) {
                            $cmes = 1;
                        } else {
                            for ($mc = $minicial; $mc <= $mfinal; $mc++) {
                                $cmes++;
                            } $cmes;
                        }$tomes = $cmes;
                    } else {
                        for ($mc = $minicial; $mc <= 12; $mc++) {
                            $imes++;
                        }$mc = 13;
                        $imes;
                        if ($aux + 1 == $afinal) {
                            if ($mc == 13) {
                                for ($mc = 1; $mc <= $mfinal; $mc++) {
                                    $fmes++;
                                }
                            }$tomes = $fmes + $imes;
                        } else {
                            while ($aux + 1 != $afinal) {
                                if ($mc == 13) {
                                    for ($mc = 1; $mc <= 12; $mc++) {
                                        $medmes++;
                                    }
                                }
                                $aux++;
                                $mc = 13;
                            } $medmes;
                        }
                        if ($mc == 13) {
                            for ($mc = 1; $mc <= $mfinal; $mc++) {
                                $fmes++;
                            }
                        } $fmes;
                        $tomes = $imes + $medmes + $fmes;
                    }$tomes;
                    ?>


                    <tr>
                        <td colspan="<?php echo $tomes + 3 ?>" align="center">No existen datos</td>                        
                    </tr>
                <?php } ?>
            </tbody>
            <tfoot align="center">
                <?php
                include_partial('tablefooter');
                ?>
            </tfoot>
        </table>
    </div>

</html>
